﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DBUtility;

namespace SQLServerDAL
{
    /// <summary>
    /// 商品类别管理
    /// </summary>
    public class RegionBase
    {
        private SqlParameter[] Para;
        private SqlConnection Conn;

        public RegionBase()
        {
            Conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction);
        }

        /// <summary>
        /// 新建目录
        /// </summary>
        public string CreateNew(Model.RegionBase region)
        {
            string result = "";
            SqlParameter[] parameters = new SqlParameter[] {
				new SqlParameter("@RegionNumber", SqlDbType.VarChar, 1024),
				new SqlParameter("@RegionName", SqlDbType.VarChar, 256),
				new SqlParameter("@RegionNameEng", SqlDbType.VarChar, 500),
				new SqlParameter("@ParentId", SqlDbType.Int),
				new SqlParameter("@Layer", SqlDbType.Int,4), 
				new SqlParameter("@RegionId", SqlDbType.Int,32,"Output"),
                new SqlParameter("@ReturnValue", SqlDbType.Int)
            };
            parameters[0].Value = region.RegionNumber;
            parameters[1].Value = region.RegionName;
            parameters[2].Value = region.RegionNameEng;
            parameters[3].Value = region.ParentId;
            parameters[4].Value = region.Layer;  
            parameters[5].Direction = ParameterDirection.Output;
            parameters[6].Direction = ParameterDirection.ReturnValue;
 
            try
            {
                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, "Region_Create", parameters);
                int status = ((parameters[6].Value != null) ? ((int)parameters[6].Value) : -1);
                if (status == 0)
                {
                    result = "succeeded" + "|" + parameters[5].Value.ToString().Trim();
                }
                else if (status == 1)
                {
                    result = "同级下名称不允许重复！"; ///storedprocedure error
                }
            }
            catch
            {
                result = "程序执行错误!"; ///procedure error
            }
            return result;
        }

        /// <summary>
        /// 修改目录
        /// </summary>
        /// <param name="subject">对象</param>
        /// <param name="intType">操作属性,0未修改父目录,1为修改父目录</param>
        public string Modify(Model.RegionBase region, bool IsChangeParent)
        {
            string result = "";
            SqlParameter[] parameters = new SqlParameter[] {
		            new SqlParameter("@RegionNumber", SqlDbType.VarChar, 1024),
		            new SqlParameter("@RegionName", SqlDbType.VarChar, 256),
		            new SqlParameter("@RegionNameEng", SqlDbType.VarChar, 500),
		            new SqlParameter("@ParentId", SqlDbType.Int),
		            new SqlParameter("@Layer", SqlDbType.Int,4), 
		            new SqlParameter("@RegionId", SqlDbType.Int),
					new SqlParameter("@IsChangeParent", SqlDbType.Bit),
                    new SqlParameter("@ReturnValue", SqlDbType.Int)
                };
            parameters[0].Value = region.RegionNumber;
            parameters[1].Value = region.RegionName;
            parameters[2].Value = region.RegionNameEng;
            parameters[3].Value = region.ParentId;
            parameters[4].Value = region.Layer;  
            parameters[5].Value = IsChangeParent;
            parameters[6].Direction = ParameterDirection.ReturnValue;
 
            try
            {
                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, "Region_Update", parameters);
                int status = ((parameters[6].Value != null) ? ((int)parameters[6].Value) : -1);
                if (status == 0)
                {
                    result = "succeeded";
                }
                else if (status == 1)
                {
                    result = "同级下名称不允许重复！"; ///storedprocedure error
                }
            }
            catch
            {
                result = "程序执行错误!"; ///procedure error
            }

            return result;
        }

        #region 获取目录信息实体
        /// <summary>
        /// 获取目录信息实体
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public Model.RegionBase GetObjById(int regionId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM RegionBase WHERE RegionId = @RegionId AND DeletionStateCode=0");

            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@RegionId", SqlDbType.Int) };
            parameters[0].Value = regionId;

            Model.RegionBase regionObj = null;
            try
            {
                SqlDataReader rd = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, strSql.ToString(), parameters);
                if (rd.Read())
                {
                    regionObj = new Model.RegionBase();
                    regionObj.RegionId = regionId;
                    regionObj.RegionNumber = rd["RegionNumber"].ToString();
                    regionObj.RegionName = rd["RegionName"].ToString();
                    regionObj.RegionNameEng = rd["RegionNameEng"].ToString();
                    regionObj.ParentId = int.Parse(rd["ParentId"].ToString());
                    regionObj.Layer = int.Parse(rd["Layer"].ToString()); 
                    regionObj.NodeType = rd["NodeType"].ToString();
                }
            }
            catch
            {
                return null;
            }
            return regionObj;
        }
        #endregion

        /// <summary>
        /// 删除目录
        /// </summary> 
        public string Delete(int regionId, bool isLogical)
        {
            string result = "";
            SqlParameter[] parameters = new SqlParameter[] {
					new SqlParameter("@RegionId", SqlDbType.Int),
					new SqlParameter("@IsLogical", SqlDbType.Bit),
                    new SqlParameter("@ReturnValue", SqlDbType.Int)
                };
            parameters[0].Value = regionId;
            parameters[1].Value = isLogical;
            parameters[2].Direction = ParameterDirection.ReturnValue;
 
            try
            {
                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, "Region_Delete", parameters);
                int status = ((parameters[2].Value != null) ? ((int)parameters[2].Value) : -1);
                switch (status)
                {
                    case 0:
                        result = "succeeded";
                        break;
                    case 1:
                        result = "节点不存在！";
                        break;
                    case -1:
                        result = "存储过程执行错误！";
                        break;
                    case 2:
                        result = "此类别下存在子类别，不允许删除！";
                        break;
                }
            }
            catch
            {
                result = "程序执行错误!";
            }

            return result;
        }
    }
}